

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <title>openspeech.optim.scheduler.tri_stage_lr_scheduler &mdash; Openspeech v0.3.0 documentation</title>



  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />










  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->


      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>

    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>


    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
</head>

<body class="wy-body-for-nav">


  <div class="wy-grid-for-nav">

    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >



            <a href="../../../../index.html" class="icon icon-home"> Openspeech



          </a>







<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>


        </div>


        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">






              <p class="caption"><span class="caption-text">GETTING STARTED</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/intro.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/hydra_configs.html">Openspeech’s Hydra configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/configs.html">Openspeech’s configurations</a></li>
</ul>
<p class="caption"><span class="caption-text">OPENSPEECH MODELS</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/Openspeech Model.html">Openspeech Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/Openspeech CTC Model.html">Openspeech CTC Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/Openspeech Encoder Decoder Model.html">Openspeech Encoder Decoder Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/Openspeech Transducer Model.html">Openspeech Transducer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/Openspeech Language Model.html">Openspeech Language Model</a></li>
</ul>
<p class="caption"><span class="caption-text">MODEL ARCHITECTURES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Conformer.html">Conformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/ContextNet.html">ContextNet</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/DeepSpeech2.html">DeepSpeech2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Jasper.html">Jasper</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Listen Attend Spell.html">Listen Attend Spell Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/LSTM LM.html">LSTM Language Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/QuartzNet.html">QuartzNet Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/RNN Transducer.html">RNN Transducer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Transformer.html">Transformer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Transformer LM.html">Transformer Language Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../architectures/Transformer Transducer.html">Transformer Transducer Model</a></li>
</ul>
<p class="caption"><span class="caption-text">CORPUS</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../corpus/AISHELL-1.html">AISHELL</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../corpus/KsponSpeech.html">KsponSpeech</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../corpus/LibriSpeech.html">LibriSpeech</a></li>
</ul>
<p class="caption"><span class="caption-text">LIBRARY REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Callback.html">Callback</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Criterion.html">Criterion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Data Augment.html">Data Augment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Feature Transform.html">Feature Transform</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Data Loaders.html">Data Loaders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Decoders.html">Decoders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Encoders.html">Encoders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Search.html">Search</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Tokenizers.html">Tokenizers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules/Metric.html">Metric</a></li>
</ul>



        </div>

      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">


      <nav class="wy-nav-top" aria-label="top navigation">

          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Openspeech</a>

      </nav>


      <div class="wy-nav-content">

        <div class="rst-content">



















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">

      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>

          <li><a href="../../../index.html">Module code</a> &raquo;</li>

      <li>openspeech.optim.scheduler.tri_stage_lr_scheduler</li>


      <li class="wy-breadcrumbs-aside">

      </li>

  </ul>


  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">

  <h1>Source code for openspeech.optim.scheduler.tri_stage_lr_scheduler</h1><div class="highlight"><pre>
<span></span><span class="c1"># MIT License</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">field</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">omegaconf</span> <span class="kn">import</span> <span class="n">DictConfig</span>
<span class="kn">from</span> <span class="nn">torch.optim</span> <span class="kn">import</span> <span class="n">Optimizer</span>

<span class="kn">from</span> <span class="nn">openspeech.dataclass.configurations</span> <span class="kn">import</span> <span class="n">LearningRateSchedulerConfigs</span>
<span class="kn">from</span> <span class="nn">openspeech.optim.scheduler</span> <span class="kn">import</span> <span class="n">register_scheduler</span>
<span class="kn">from</span> <span class="nn">openspeech.optim.scheduler.lr_scheduler</span> <span class="kn">import</span> <span class="n">LearningRateScheduler</span>


<div class="viewcode-block" id="TriStageLRSchedulerConfigs"><a class="viewcode-back" href="../../../../modules/Optim.html#openspeech.optim.scheduler.tri_stage_lr_scheduler.TriStageLRSchedulerConfigs">[docs]</a><span class="nd">@dataclass</span>
<span class="k">class</span> <span class="nc">TriStageLRSchedulerConfigs</span><span class="p">(</span><span class="n">LearningRateSchedulerConfigs</span><span class="p">):</span>
    <span class="n">scheduler_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="s2">&quot;tri_stage&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Name of learning rate scheduler.&quot;</span><span class="p">}</span>
    <span class="p">)</span>
    <span class="n">init_lr</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="mf">1e-7</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Initial learning rate.&quot;</span><span class="p">}</span>
    <span class="p">)</span>
    <span class="n">init_lr_scale</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Initial learning rate scale.&quot;</span><span class="p">}</span>
    <span class="p">)</span>
    <span class="n">final_lr_scale</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Final learning rate scale&quot;</span><span class="p">}</span>
    <span class="p">)</span>
    <span class="n">phase_ratio</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="s2">&quot;(0.1, 0.4, 0.5)&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Automatically sets warmup/hold/decay steps to the ratio &quot;</span>
                                                     <span class="s2">&quot;specified here from max_updates. the ratios must add up to 1.0&quot;</span><span class="p">}</span>
    <span class="p">)</span>
    <span class="n">total_steps</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span>
        <span class="n">default</span><span class="o">=</span><span class="mi">400000</span><span class="p">,</span> <span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;help&quot;</span><span class="p">:</span> <span class="s2">&quot;Total training steps.&quot;</span><span class="p">}</span>
    <span class="p">)</span></div>


<div class="viewcode-block" id="TriStageLRScheduler"><a class="viewcode-back" href="../../../../modules/Optim.html#openspeech.optim.scheduler.tri_stage_lr_scheduler.TriStageLRScheduler">[docs]</a><span class="nd">@register_scheduler</span><span class="p">(</span><span class="s2">&quot;tri_stage&quot;</span><span class="p">,</span> <span class="n">dataclass</span><span class="o">=</span><span class="n">TriStageLRSchedulerConfigs</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">TriStageLRScheduler</span><span class="p">(</span><span class="n">LearningRateScheduler</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Tri-Stage Learning Rate Scheduler. Implement the learning rate scheduler in &quot;SpecAugment&quot;</span>

<span class="sd">    Similar to inverse_squre_root scheduler, but tri_stage learning rate employs</span>
<span class="sd">    three stages LR scheduling:</span>

<span class="sd">        - warmup stage, starting from `lr` * `init_lr_scale`, linearly</span>
<span class="sd">          increased to `lr` in `warmup_steps` iterations</span>
<span class="sd">        - hold stage, after `warmup_steps`, keep the LR as `lr` for `hold_steps`</span>
<span class="sd">          iterations</span>
<span class="sd">        - decay stage, after hold stage, decay LR exponetially to</span>
<span class="sd">          `lr` * `final_lr_scale` in `decay_steps`;</span>
<span class="sd">          after that LR is keep as `final_lr_scale` * `lr`</span>

<span class="sd">    During warmup::</span>
<span class="sd">      init_lr = cfg.init_lr_scale * cfg.lr</span>
<span class="sd">      lrs = torch.linspace(init_lr, cfg.lr, cfg.warmup_steps)</span>
<span class="sd">      lr = lrs[update_num]</span>

<span class="sd">    During hold::</span>
<span class="sd">      lr = cfg.lr</span>

<span class="sd">    During decay::</span>
<span class="sd">      decay_factor = - math.log(cfg.final_lr_scale) / cfg.decay_steps</span>
<span class="sd">      lr = cfg.lr * exp(- (update_num - warmup_steps - decay_steps) * decay_factor)</span>

<span class="sd">    After that::</span>
<span class="sd">      lr = cfg.lr * cfg.final_lr_scale</span>

<span class="sd">    Args:</span>
<span class="sd">        optimizer (Optimizer): wrapped optimizer.</span>
<span class="sd">        configs (DictConfig): configuration set.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">optimizer</span><span class="p">:</span> <span class="n">Optimizer</span><span class="p">,</span>
            <span class="n">configs</span><span class="p">:</span> <span class="n">DictConfig</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">TriStageLRScheduler</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">optimizer</span><span class="p">,</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">init_lr</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">phase_ratio</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">phase_ratio</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">warmup_steps</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">total_steps</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phase_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hold_steps</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">total_steps</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phase_ratio</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decay_steps</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">total_steps</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phase_ratio</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">peak_lr</span> <span class="o">=</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">lr</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_lr</span> <span class="o">=</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">init_lr_scale</span> <span class="o">*</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">lr</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">final_lr</span> <span class="o">=</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">final_lr_scale</span> <span class="o">*</span> <span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">lr</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">warmup_rate</span> <span class="o">=</span> <span class="p">(</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">peak_lr</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_lr</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">warmup_steps</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">warmup_steps</span> <span class="o">!=</span> <span class="mi">0</span>
            <span class="k">else</span> <span class="mi">0</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decay_factor</span> <span class="o">=</span> <span class="o">-</span><span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">configs</span><span class="o">.</span><span class="n">lr_scheduler</span><span class="o">.</span><span class="n">final_lr_scale</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">decay_steps</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_lr</span>

    <span class="k">def</span> <span class="nf">_decide_stage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">warmup_steps</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span>

        <span class="n">offset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">warmup_steps</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">&lt;</span> <span class="n">offset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">hold_steps</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">-</span> <span class="n">offset</span>

        <span class="n">offset</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hold_steps</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">&lt;=</span> <span class="n">offset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">decay_steps</span><span class="p">:</span>
            <span class="c1"># decay stage</span>
            <span class="k">return</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">-</span> <span class="n">offset</span>

        <span class="n">offset</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decay_steps</span>

        <span class="k">return</span> <span class="mi">3</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">-</span> <span class="n">offset</span>

    <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val_loss</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">FloatTensor</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
        <span class="n">stage</span><span class="p">,</span> <span class="n">steps_in_stage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_decide_stage</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">stage</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_lr</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">warmup_rate</span> <span class="o">*</span> <span class="n">steps_in_stage</span>
        <span class="k">elif</span> <span class="n">stage</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">peak_lr</span>
        <span class="k">elif</span> <span class="n">stage</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">peak_lr</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">decay_factor</span> <span class="o">*</span> <span class="n">steps_in_stage</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">stage</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">final_lr</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Undefined stage&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">set_lr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lr</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update_step</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lr</span></div>
</pre></div>

           </div>

          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, Kim, Soohwan and Ha, Sangchun and Cho, Soyoung.

    </p>
  </div>



    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a

    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>

    provided by <a href="https://readthedocs.org">Read the Docs</a>.

</footer>
        </div>
      </div>

    </section>

  </div>


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>






</body>
</html>